<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Nagios整合微信订阅号报警 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Nagios整合微信订阅号报警</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境：rhel6.5 selinux 和 iptables 关闭。要求能上外网的（虚拟机亲测可用）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是nagios服务与微信订阅号的整合过程，最终实现当服务或主机出现故障，自动调用微信报警。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重点讲述如何配置微信公众平台私有接口，至于nagios服务的配置请大家参照nagios官方文档进行，此处不再赘述。（www.nagios.org）</p>
<h2 id="1-下载微信公众平台私有接口"><a href="#1-下载微信公众平台私有接口" class="headerlink" title="1.下载微信公众平台私有接口"></a>1.下载微信公众平台私有接口</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">yum install -y git</div><div class="line">git <span class="built_in">clone</span> https://github.com/lealife/WeiXin-Private-API</div></pre></td></tr></table></figure>
<h2 id="2-修改微信公众平台私有接口代码，以配合nagios报警"><a href="#2-修改微信公众平台私有接口代码，以配合nagios报警" class="headerlink" title="2.修改微信公众平台私有接口代码，以配合nagios报警"></a>2.修改微信公众平台私有接口代码，以配合nagios报警</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">cp -r WeiXin-Private-API /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div><div class="line">chown -R nagios.nagios /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nagios/libexec/weixin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改config.php文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$G_CONFIG</span>[<span class="string">"weiXin"</span>] = array(</div><div class="line"></div><div class="line">        <span class="string">'account'</span> =&gt; <span class="string">'微信公众平台登录帐号'</span>,<span class="comment">#填写你注册的微信订阅号的帐号和密码</span></div><div class="line"></div><div class="line">        <span class="string">'password'</span> =&gt; <span class="string">'微信公众平台登录密码'</span>,</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改test.php文件,只保留如下几行即可：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">require <span class="string">"config.php"</span>;</div><div class="line">require <span class="string">"include/WeiXin.php"</span>;</div><div class="line"></div><div class="line"><span class="variable">$weiXin</span> = new WeiXin(<span class="variable">$G_CONFIG</span>[<span class="string">'weiXin'</span>]);</div><div class="line"></div><div class="line"><span class="variable">$testFakeId</span> = <span class="string">"<span class="variable">$argv</span>[1]"</span>;<span class="comment">#微信好友ID号，这里通过nagios传入</span></div><div class="line"></div><div class="line"><span class="variable">$msg</span> = `cat /usr/<span class="built_in">local</span>/nagios/var/nagios.msg`;<span class="comment">#要发送的报警信息，由nagios传入</span></div><div class="line"></div><div class="line">print_r(<span class="variable">$weiXin</span>-&gt;send(<span class="variable">$testFakeId</span>, <span class="string">"<span class="variable">$msg</span>"</span>));<span class="comment">#给微信好友发送信息</span></div></pre></td></tr></table></figure>
<h2 id="3-整合nagios和微信公共平台私有接口"><a href="#3-整合nagios和微信公共平台私有接口" class="headerlink" title="3.整合nagios和微信公共平台私有接口"></a>3.整合nagios和微信公共平台私有接口</h2><h3 id="增加微信报警选项-templates-cfg"><a href="#增加微信报警选项-templates-cfg" class="headerlink" title="增加微信报警选项: templates.cfg"></a>增加微信报警选项: templates.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/templates.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 define contact{…} 部分,将以下两行:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">host_notification_commands notify-host-by-email</div><div class="line">service_notification_commands notify-service-by-email</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">host_notification_commands notify-host-by-email,notify-host-by-weixin</div><div class="line">service_notification_commands notify-service-by-email,notify-service-by-weixin</div></pre></td></tr></table></figure>
<h3 id="增加调用命令-commands-cfg"><a href="#增加调用命令-commands-cfg" class="headerlink" title="增加调用命令: commands.cfg"></a>增加调用命令: commands.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/commands.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在该文件的最后增加以下部分:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">        <span class="comment">####notify-service-by-weixin</span></div><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">                command_name notify-service-by-weixin</div><div class="line">                command_line /usr/bin/<span class="built_in">printf</span> <span class="string">"%b"</span> <span class="string">"***** Nagios *****\n\nNotification Type: <span class="variable">$NOTIFICATIONTYPE</span>$\nHost: <span class="variable">$HOSTNAME</span>$\nState: <span class="variable">$HOSTSTATE</span>$\nAddress: <span class="variable">$HOSTADDRESS</span>$\nInfo: <span class="variable">$HOSTOUTPUT</span>$\n\nDate/Time: <span class="variable">$LONGDATETIME</span>$\n"</span> &gt; /usr/<span class="built_in">local</span>/nagios/var/nagios.msg &amp;&amp; /usr/bin/php /usr/<span class="built_in">local</span>/nagios/libexec/weixin/test.php <span class="variable">$CONTACTADDRESS1</span>$  &amp;&gt;/dev/null        </div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="comment">####notify-host-by-weixin        </span></div><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">                command_name notify-host-by-weixin</div><div class="line">                command_line /usr/bin/<span class="built_in">printf</span> <span class="string">"%b"</span> <span class="string">"***** Nagios *****\n\nNotification Type: <span class="variable">$NOTIFICATIONTYPE</span>$\nHost: <span class="variable">$HOSTNAME</span>$\nState: <span class="variable">$HOSTSTATE</span>$\nAddress: <span class="variable">$HOSTADDRESS</span>$\nInfo: <span class="variable">$HOSTOUTPUT</span>$\n\nDate/Time: <span class="variable">$LONGDATETIME</span>$\n"</span> &gt; /usr/<span class="built_in">local</span>/nagios/var/nagios.msg &amp;&amp; /usr/bin/php /usr/<span class="built_in">local</span>/nagios/libexec/weixin/test.php <span class="variable">$CONTACTADDRESS1</span>$  &amp;&gt;/dev/null        </div><div class="line">        &#125;</div></pre></td></tr></table></figure>
<h3 id="修改联系人选项-contact-cfg"><a href="#修改联系人选项-contact-cfg" class="headerlink" title="修改联系人选项: contact.cfg"></a>修改联系人选项: contact.cfg</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 /usr/local/nagios/etc/objects/contact.cfg</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 define contact{…} 部分增加如下一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">address     11206***<span class="comment">#微信好友ID，登录微信公众平台网页版，在用户管理中点击你要发微信的好友，此时在地址上显示的fakeid就是微信好友的ID。</span></div></pre></td></tr></table></figure>
<h3 id="重载nagios配置"><a href="#重载nagios配置" class="headerlink" title="重载nagios配置"></a>重载nagios配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service nagios reload</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nagios%E6%95%B4%E5%90%88%E5%BE%AE%E4%BF%A1%E8%AE%A2%E9%98%85%E5%8F%B7%E6%8A%A5%E8%AD%A6/01.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nagios%E6%95%B4%E5%90%88%E5%BE%AE%E4%BF%A1%E8%AE%A2%E9%98%85%E5%8F%B7%E6%8A%A5%E8%AD%A6/02.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nagios/4. Nagios整合微信订阅号报警/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
